﻿using System;
using Castle.Windsor;
using Castle.Windsor.Installer;
using GdNet.Applications.DataGeneration.DataAssistants;
using GdNet.Integrations.Castle;
using GdNet.WebFramework.Data;
using NHibernate;

namespace GdNet.Applications.DataGeneration
{
    class Program
    {
        private static IWindsorContainer _container;

        static void Main(string[] args)
        {
            BootstrapContainer();

            var dataRepositories = _container.Resolve<DataRepositories>();

            using (var nhSession = _container.Resolve<ISession>())
            {
                using (var transaction = nhSession.BeginTransaction())
                {
                    Console.WriteLine("-------------------");
                    CatalogAssistant.GenerateSampleCatalogs(dataRepositories.Catalog);

                    Console.WriteLine("-------------------");
                    TranslationAssistant.GenerateSampleTranslations(dataRepositories.Translation);

                    Console.WriteLine("-------------------");
                    ContentItemAssistant.GenerateSampleContents(dataRepositories.ContentItem, dataRepositories.Catalog);

                    Console.WriteLine("-------------------");
                    ModuleAssistant.GeneratedSampleModules(dataRepositories.Module);

                    Console.WriteLine("-------------------");
                    FastTestAssistant.TestRepositories(dataRepositories);

                    transaction.Commit();
                }
            }

            Console.WriteLine("-------------------");
            Console.WriteLine("Press enter to exit...");
            Console.ReadLine();
        }

        private static void BootstrapContainer()
        {
            _container = new WindsorContainer().Install(FromAssembly.Containing<CastleIntegrations>());
        }

        //private static INHibernateSessionManager BuildSessionManager()
        //{
        //    string currentDirectory = Directory.GetCurrentDirectory();
        //    var sessionManagerBuilder = NHibernateSessionManagerBuilder.NewInstance();

        //    sessionManagerBuilder.UseConfigurationFile(Path.Combine(currentDirectory, "AppData", "hibernate.cfg.xml"));
        //    sessionManagerBuilder.AddInterceptor(new EntityWithModificationInterceptor());

        //    string mappingAssemblies = ConfigurationManager.AppSettings["MappingFiles"];
        //    foreach (var mappingAssemblyFile in mappingAssemblies.Split(';'))
        //    {
        //        sessionManagerBuilder.WithMappingFile(Path.Combine(currentDirectory, mappingAssemblyFile));
        //    }

        //    return sessionManagerBuilder.BuildSessionManager();
        //}
    }
}
